<?php
include 'curlLib.php';
class speedPostContract {

    var $dateTime;
    var $location;
    var $status;
    var $reason;

}

class Uuch_Track_SpeedPostAPI {

    private $ch;
    
    private $num1;
    
    private $num2;
    
    public function __construct() {
        $this->ch = curl_init();
        $this->num1 = 41;
        $this->num2 = 86;
    }

    private function getArray($table) {
        $table = preg_replace("'<table[^>]*?>'si", "", $table);
        $table = preg_replace("'<tr[^>]*?>'si", "", $table);
        $table = preg_replace("'<td[^>]*?>'si", "", $table);
        $table = str_replace("</tr>", "{tr}", $table);
        $table = str_replace("</td>", "{td}", $table);
        //去掉 HTML 标记
        $table = preg_replace("'<[\/\!]*?[^<>]*?>'si", "", $table);
        //去掉空白字符 
        $table = preg_replace("'([\r\n])[\s]+'", "", $table);
        // $table = str_replace(" ", "", $table);
        // $table = str_replace(" ", "", $table);
        $table = explode('{tr}', $table);
        array_pop($table);
        foreach ($table as $key => $tr) {
            $td = explode('{td}', $tr);
            array_pop($td);
            $td_array[] = $td;
        }
        return $td_array;
    }

    function getSessionID($trackItem) {
        $url = "http://www.speedpost.com.sg/index.php?quickTool=TrackItem";
/*
txt_track_items_86:EQ303136014SG
txt_track_items_41:
btnTrack:Track
option:com_content_left
field_item:86
controller:tracking
task:tracking
limitTrackItem:10
 */
        $request = "txt_track_items_$this->num1=" . $trackItem .
                "&txt_track_items_86&field_item=$this->num1&btnTrack=Track&option=com_content_left
                    &controller=tracking&task=tracking&limitTrackItem=10";
        //$curl = new CURL();
        //$result = $curl->doRequest('POST', $url, $request);
        curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_HEADER, 0);
        curl_setopt($this->ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookie.txt');
        curl_setopt($this->ch, CURLOPT_COOKIEFILE,  dirname(__FILE__) . '/cookie.txt');
        //if ($method == 'POST') {
        curl_setopt($this->ch, CURLOPT_POST, 1);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request);
        //}
        $data = curl_exec($this->ch);       
        if(!strpos($data, $trackItem)){
            $matchs = array();
            if(preg_match('/txt_track_items_\d+/', $data,$matchs)){
                $lis = explode('_', $matchs[0]);
                $this->num1 = $lis[count($lis)-1];   
            }
            $request = "txt_track_items_$this->num1=" . $trackItem .
                "&txt_track_items_86&field_item=$this->num1&btnTrack=Track&option=com_content_left
                    &controller=tracking&task=tracking&limitTrackItem=10";
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request);
            $data = curl_exec($this->ch);
        }
    }

    function query($trackItem) {
         $this->getSessionID($trackItem);
        $url = "http://www.speedpost.com.sg/index.php";
        /*option:com_content_left
controller:tracking
task:trackdetail
track_item_86:EQ303136014SG
         */
        curl_setopt($this->ch, CURLOPT_URL, $url);
        $request = "option=com_content_left&controller=tracking&task=trackdetail&track_item_$this->num1=$trackItem";
        //$curl = new CURL();
        //$result = $curl->doRequest('POST', $url, $request);  
        //$this->ch = curl_init();
        
        //if ($method == 'POST') {
            curl_setopt($this->ch, CURLOPT_POST, 1);
            curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request);
        //}
        $data = curl_exec($this->ch);
        curl_close($this->ch);
        $result = $data;
        $seletpreg = '%<table.*?>(.*?)</table>%si';
        preg_match_all($seletpreg, $result, $match);
        $resultArray = $this->getArray($match[0][0]);
        unset($resultArray[0]);
        $resultList = array();
        foreach ($resultArray as $value) {
            $model = new speedPostContract();
            $model->dateTime = trim($value[0]);
            $model->location = trim($value[1]);
            $model->status = trim($value[2]);
            $model->reason = trim($value[3]);
            array_push($resultList, $model);
        }
        return $resultList;
    }

}

?>
